package org.grooss.image3d;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

//test
public class MainActivity extends ActionBarActivity implements OnClickListener  {
	
	private static final int SELECT_IMAGE = 1;
	Button openGalery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openGalery = (Button) findViewById(R.id.openGalery);
        openGalery.setOnClickListener(this);
        
        
        
    }
    
    @Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.openGalery:
      Intent gallery = new Intent(
      Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
      startActivityForResult(gallery, SELECT_IMAGE);
      break;
     
      default:
        break;
    }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
     
      if(resultCode==RESULT_OK && requestCode==SELECT_IMAGE){
       Uri selectedImage=data.getData();
       String path=getPath(selectedImage);
     
       Bitmap bitmapImage=BitmapFactory.decodeFile(path);
       ImageView image=(ImageView)findViewById(R.id.image);
       image.setImageBitmap(bitmapImage);
     
      }
    }
     
    public String getPath(Uri uri){
      String[] filePathColumn={MediaStore.Images.Media.DATA};
     
      Cursor cursor=getContentResolver().query(uri, filePathColumn, null, null, null);
      cursor.moveToFirst();
      int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
     
      return cursor.getString(columnIndex);
    }

    public static Bitmap doGreyscale(Bitmap src) {
        // constant factors
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;
     
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        // pixel information
        int A, R, G, B;
        int pixel;
     
        // get image size
        int width = src.getWidth();
        int height = src.getHeight();
     
        // scan through every single pixel
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
     
        // return final image
        return bmOut;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
